<?php
/**
 * name：miss
 * time：2017/10/10/19：18
 * message：第一天的晚上 写详情页的控制器
 */
namespace Home\Controller; //命名空间
use Think\Controller; //加载父类

/**
* 商品详情页的控制器
*/
class GoodsInfoController extends Controller
{
	

	/**
	 * 商品详情页 
	 */
	public function goodsInfo()
	{
		//判断是否是分享过来的
		$type_id  = I('get.id');
        if(cookie('id') == null){
        		redirect('https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5b3db39546bb1360&redirect_uri=http%3a%2f%2fwww.jsyuanma.top%2findex.php%2fHome%2fGoodsInfo%2fweixin_interface%2fid%2f'.$type_id.'&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect');
        }else{
        	// 展示商品详情
			// 查询商品id的详情图片
			$goods_url = M('goods_detail')->where(array('goods_id'=>$type_id))->select();
			// 获取轮播图图片
			$goods_carousel = M('goods_images') -> where(array('goods_id'=>$type_id)) -> select();
			// 查询商品id的详情内容
			$goods_info = M('goods') -> where(array('id'=>$type_id)) -> select();
	
			// dump($goods_info);
	
			// 读取商品的颜色，转换为数组
			foreach ($goods_info as $val) {
				$color_type = explode('_',$val['color_type']);
			}
			// dump($color_type);
	
	
			// 查询商品总分类
			$type_name = M('goods_type_parent')->select();
	
			//输出颜色的分类
			$this -> assign('color_type',$color_type);
			
			//输出总分类名字
			$this -> assign('type_name',$type_name);
	
			// 输出商品详情图片路径
			$this -> assign('goods_carousel',$goods_carousel);
			$this -> assign('goods_url',$goods_url);
			// 输出商品详情
			$this -> assign('goods_info',$goods_info);
			$this -> display();

        }
	}

	 public function  weixin_interface(){
        //首先判断session中是否存在用户名和头像。如果存在则不用使用oauth请求access_token
        	$goods_id = I('get.id');
            $date = getdate(date("U"));
            //获取当前登录时间
            $current_date = $date[year].'-'.($date[mon]+1).'-'.$date[mday]." "."  ".$date[hours].':'.$date[minutes].':'.$date[seconds];
            //获取code
            $code  = $_GET['code'];

            //公众号唯一id
            $appid = 'wx5b3db39546bb1360';
            $appsecret = '9ff6efc83ad1e67a07403fe402ccd1b8';

            //通过code获取access_token
            $get_access_token = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
            $text = file_get_contents($get_access_token);

            //刷新access_token
            $refresh_token = json_decode($text,true)['refresh_token'];
            $text2 = file_get_contents('https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$refresh_token);
            //将json字符串转换为数组
            $access_token =  json_decode($text2,true)['access_token'];
            //用户唯一id
            $openid       =  json_decode($text,true)['openid'];

            //获取微信个人信息
            $personInfo   =  file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');

            $name     = json_decode($personInfo,true)['nickname'];
            $imageUrl = json_decode($personInfo,true)['headimgurl'];
            //将昵称和头像地址放入到cookie里面
            cookie('name',$name,7000);
            cookie('imageUrl',$imageUrl,7000);
            //首先判断是否存在用户
            $result = M("User") -> where(array('nickname'=>$name,'head_pic'=>$imageUrl)) -> find();
            
            if(!$result){   

                //获取ip地址
                $ip = get_client_ip();

                //存入数据库
                M("User") -> add(array('oauth'=>'weixin','head_pic'=>$imageUrl,'nickname'=>$name,'last_login'=>$current_date,'last_ip'=>$ip));
                $id = M("User") -> where(array('nickname'=>$name)) -> find();
                cookie('id',$id['user_id'],7000);
                redirect('http://www.jsyuanma.top/index.php/Home/GoodsInfo/goodsInfo/id/'.$goods_id);
               
            }else{
                cookie('id',$result['user_id'],7000);
                redirect('http://www.jsyuanma.top/index.php/Home/GoodsInfo/goodsInfo/id/'.$goods_id);
            }
    }


	//错误提示信息
    public function _empty(){
        echo "您访问的页面不存在！";
    }

}
